package com.star.modules.sys.service;

import com.jeesite.common.collect.MapUtils;
import com.jeesite.common.entity.Page;
import com.jeesite.common.mapper.BeanMapper;
import com.jeesite.modules.sys.entity.DictData;
import com.jeesite.modules.sys.utils.DictUtils;
import com.star.modules.sys.base.BizDataResult;
import com.star.modules.sys.dao.ShInfoDao;
import com.star.modules.sys.entity.ShFloatWin;
import com.star.modules.sys.entity.ShInfo;
import com.star.modules.sys.enums.DictEnum;
import com.star.modules.sys.enums.ResultEnum;
import com.star.modules.sys.excetion.ApiException;
import com.star.modules.sys.vo.BottomVo;
import com.star.modules.sys.vo.FloatWinVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 首页服务类
 */
@Service
@Transactional(readOnly = true)
public class BottomService {

    @Autowired
    private ShInfoDao shInfoDao;

    @Autowired
    private ShFloatWinService shFloatWinService;
    //经纪团队
    public final static int BROKET_TYPE = 5;
    public final static int BROKET_PAGE_SIZE = 5;
    public final static int BROKET_PAGE_NO = 1;

    private final static String FLOAT_INFO_ID = "1";
    private final static int SPLIT_CHAR_POS = 9;

    /**
     * 底部动态信息
     *
     * @return
     */
    public BizDataResult getInfo() {
        Map<String, Object> result = MapUtils.newHashMap();
        try {
            BottomVo bottomVo = new BottomVo();
            List<DictData> dictDatas = DictUtils.getDictList(DictEnum.AREA_CONDITION.getCode());
            bottomVo.setAreaConditions(BeanMapper.mapList(dictDatas, BottomVo.AreaCondition.class));
            ShInfo infoQuery = new ShInfo();
            infoQuery.setType(BROKET_TYPE);
            infoQuery.setStatus(ShInfo.STATUS_NORMAL);
            infoQuery.setPage(new Page<>(BROKET_PAGE_NO, BROKET_PAGE_SIZE));
            bottomVo.setBrokets(BeanMapper.mapList(shInfoDao.findList(infoQuery), BottomVo.Broket.class));
            result.put("bottomVo", bottomVo);
            ShFloatWin shFloatWin = shFloatWinService.get(FLOAT_INFO_ID);
            FloatWinVo floatWinVo = BeanMapper.map(shFloatWin, FloatWinVo.class);
            if (floatWinVo.getAddr().length() > SPLIT_CHAR_POS) {
                StringBuilder sb = new StringBuilder(floatWinVo.getAddr());
                sb.insert(SPLIT_CHAR_POS, "<br/>");
                floatWinVo.setMinAddr(sb.toString());
            } else {
                floatWinVo.setMinAddr(floatWinVo.getAddr());
            }
            result.put("floatWin", floatWinVo);
        } catch (Exception e) {
            throw new ApiException(e);
        }
        return BizDataResult.createResult(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), result);
    }
}
